//
//  ViewController.m
//  跑马灯
//
//  Created by huahua on 16/12/28.
//  Copyright © 2016年 os1. All rights reserved.
//

#import "ViewController.h"
#import "PaoMaDengView.h"
#import "SettingView.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width  // 屏幕宽度
#define kScreenHeight [UIScreen mainScreen].bounds.size.height  // 屏幕高度

@interface ViewController ()<UITextFieldDelegate,SettingViewDelegate>
{
    PaoMaDengView *_paomaView;  //跑马灯视图
    UITextField *_paomaTextField;  //文字输入框
    UIButton *_settingBtn;  //设置按钮
    SettingView *_settingView;  //设置视图
    
    NSString *_paomatext;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _paomaView = [[PaoMaDengView alloc] initWithFrame:self.view.frame];
    _paomatext = @"请在输入框中输入文字！";
    [self.view addSubview:_paomaView];
    
    _paomaTextField = [[UITextField alloc] initWithFrame:CGRectZero];
    _paomaTextField.delegate = self;
    _paomaTextField.layer.cornerRadius = 5;
    _paomaTextField.layer.borderWidth = 1;
    _paomaTextField.textColor = [UIColor whiteColor];
    _paomaTextField.layer.borderColor = [UIColor whiteColor].CGColor;
    [self.view addSubview:_paomaTextField];
    
    _settingBtn = [[UIButton alloc] initWithFrame:CGRectZero];
    [_settingBtn setImage:[UIImage imageNamed:@"设置"] forState:UIControlStateNormal];
    [_settingBtn addTarget:self action:@selector(settingBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_settingBtn];
}

/** 设置按钮点击事件*/
- (void)settingBtnClick{
    
    _settingView = [[SettingView alloc] initWithFrame:CGRectZero];
    _settingView.delegation = self;
    
    [self.view addSubview:_settingView];
    
}

/** 设置子控件的frame*/
- (void)viewWillLayoutSubviews{
    
    [super viewWillLayoutSubviews];
    
    _paomaView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    _paomaTextField.frame = CGRectMake(50, 20, kScreenWidth-120, 30);
    
    _settingBtn.frame = CGRectMake(CGRectGetMaxX(_paomaTextField.frame)+20, 20, 30, 30);
    
    _settingView.frame = CGRectMake(20, 20, kScreenWidth-40, kScreenHeight-40);
}

#pragma mark - SettingViewDelegate
- (void)settingWithTextColor:(UIColor *)textColor textFont:(NSInteger)textFont bgColor:(UIColor *)bgColor{
    
    _paomaView.textFont = textFont;  //这里注意要先设置字体大小
    _paomaView.title = _paomatext;
    _paomaView.textColor = textColor;
    _paomaView.bgColor = bgColor;
    
    BOOL isEqual = [self _compareRGBAColor1:_paomaView.bgColor withColor2:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
    if (isEqual == 0) {
        
        _paomaTextField.textColor = [UIColor whiteColor];
        _paomaTextField.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    else{
        
        _paomaTextField.textColor = [UIColor blackColor];
        _paomaTextField.layer.borderColor = [UIColor blackColor].CGColor;
    }
}

/** 比较两个颜色是否一样*/
- (BOOL)_compareRGBAColor1:(UIColor *)color1 withColor2:(UIColor *)color2 {
    
    CGFloat red1,red2,green1,green2,blue1,blue2,alpha1,alpha2;
    //取出color1的背景颜色的RGBA值
    [color1 getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
    //取出color2的背景颜色的RGBA值
    [color2 getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2];
    
    if ((red1 == red2)&&(green1 == green2)&&(blue1 == blue2)&&(alpha1 == alpha2)) {
        return YES;
    } else {
        return NO;
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
    _paomaView.title = textField.text;
    _paomatext = textField.text;
    textField.text = @"";
    
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

@end
